/*
 * Created by JFormDesigner on Mon Jul 01 08:30:37 CST 2024
 */

package scct.yzx.ui;

import scct.yzx.dao.UserDao;
import scct.yzx.model.User;
import scct.yzx.utils.Tools;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author lenovo
 */
public class DlgUserUpdate extends JDialog {
    int id;
    public DlgUserUpdate(Window owner,int id) {
        super(owner);
        this.id = id;
        initComponents();
        initView();
    }

    private void initView(){
//        根据id查询用户，把用户的信息添加到
        UserDao userDao = new UserDao();
        User user = userDao.getUserById(id);
        txtName.setText(user.getUsername());
        txtPass.setText(user.getPassword());

    }

    private void btnUpdate(ActionEvent e) {
        // TODO add your code here
        String username = txtName.getText();
        String password = txtPass.getText();
        String createtime = Tools.getCreatetime();
        if (!username.matches("^\\w{3,10}$") || !password.matches("^\\w{3,10}$")){
            JOptionPane.showMessageDialog(this,"请输入合法用户名和密码");
            return;
        }
        //      验证用户名重复
        UserDao userDao = new UserDao();
        if (userDao.updateUser(id,password,createtime)){
            JOptionPane.showMessageDialog(this,"修改成功");
            dispose();
        }
    }

    private void btnCancle(ActionEvent e) {
        // TODO add your code here
        this.dispose();
    }





    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - yzx
        label1 = new JLabel();
        txtName = new JTextField();
        label2 = new JLabel();
        txtPass = new JTextField();
        btnUpdate = new JButton();
        btnCancle = new JButton();

        //======== this ========
        setTitle("\u4fee\u6539\u7528\u6237");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u7528\u6237\u540d");
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(75, 45), label1.getPreferredSize()));

        //---- txtName ----
        txtName.setEditable(false);
        contentPane.add(txtName);
        txtName.setBounds(160, 40, 105, txtName.getPreferredSize().height);

        //---- label2 ----
        label2.setText("\u5bc6\u7801");
        contentPane.add(label2);
        label2.setBounds(new Rectangle(new Point(80, 100), label2.getPreferredSize()));
        contentPane.add(txtPass);
        txtPass.setBounds(160, 95, 110, txtPass.getPreferredSize().height);

        //---- btnUpdate ----
        btnUpdate.setText("\u4fee\u6539");
        btnUpdate.addActionListener(e -> btnUpdate(e));
        contentPane.add(btnUpdate);
        btnUpdate.setBounds(new Rectangle(new Point(75, 160), btnUpdate.getPreferredSize()));

        //---- btnCancle ----
        btnCancle.setText("\u53d6\u6d88");
        btnCancle.addActionListener(e -> btnCancle(e));
        contentPane.add(btnCancle);
        btnCancle.setBounds(new Rectangle(new Point(210, 160), btnCancle.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(400, 300);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - yzx
    private JLabel label1;
    private JTextField txtName;
    private JLabel label2;
    private JTextField txtPass;
    private JButton btnUpdate;
    private JButton btnCancle;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
